import json

from django.http import HttpResponse
from django.shortcuts import render

# render请求
from green import models



def line1(request):
    return render(request, "seven/line1.html")


def line2(request):
    return render(request, "seven/line2.html")


def line3(request):
    return render(request, "seven/line3.html")


def line4(request):
    return render(request, "seven/line4.html")


def line5(request):
    return render(request, "seven/line5.html")


def line6(request):
    return render(request, "seven/line6.html")


def line7(request):
    return render(request, "seven/line7.html")


def line8(request):
    return render(request, "seven/line8.html")


def seven(request):
    return render(request, "seven/seven.html")
    # return render(request, "six/six.html")


def initBigJsonForSeven(request):
    f = open('templates/six/bigJson.json', 'r', encoding='utf-8')
    bigJson = f.read()
    f.close()
    print("bigjson的类型为：" + str(type(bigJson)))
    oneDict = json.loads(bigJson)

    print("t2的类型为：" + str(type(oneDict)))

    monthList = ['2019/12', '2020/1', '2020/2', '2020/3', '2020/4', '2020/5', '2020/6', '2020/7']

    line1 = ["Sta65", "Sta49", "Sta149", "Sta74", "Sta128", "Sta34", "Sta106", "Sta110", "Sta97", "Sta80",
             "Sta89", "Sta64", "Sta150", "Sta154", "Sta107", "Sta83", "Sta108", "Sta159", "Sta1"]
    line2 = ["Sta51", "Sta105", "Sta24", "Sta139", "Sta71", "Sta57", "Sta76", "Sta52", "Sta68", "Sta151", "Sta48",
             "Sta27",
             "Sta81", "Sta127", "Sta123", "Sta47", "Sta18", "Sta79", "Sta78", "Sta53", "Sta163", "Sta9", "Sta129"]
    line3 = [ "Sta143", "Sta156", "Sta61", "Sta50", "Sta119", "Sta66", "Sta12", "Sta161", "Sta21", "Sta133",
             "Sta22",
             "Sta138", "Sta41", "Sta30", "Sta67", "Sta144",  "Sta29", "Sta126", "Sta40", "Sta131",
             "Sta39",
             "Sta100", "Sta167", "Sta113", "Sta141", "Sta142", "Sta158", "Sta44", "Sta117", "Sta147", "Sta42", "Sta35",
             "Sta109", "Sta33", "Sta112", "Sta153", "Sta125", "Sta121", "Sta11"]
    line4 = ["Sta84", "Sta59", "Sta19", "Sta62", "Sta165", "Sta58", "Sta38"]

    line5 = ["Sta54", "Sta69", "Sta16", "Sta37", "Sta132", "Sta96", "Sta10", "Sta43"]

    line6 = ["Sta157", "Sta114", "Sta168", "Sta135", "Sta134", "Sta85", "Sta2", "Sta4", "Sta103", "Sta145", "Sta88",
              "Sta87", "Sta94", "Sta160", "Sta76", "Sta8", "Sta75", "Sta102"]

    line7 = ["Sta28", "Sta124", "Sta99", "Sta166", "Sta36", "Sta122", "Sta77", "Sta140", "Sta111", "Sta13", "Sta70",
              "Sta55", "Sta20", "Sta23", "Sta56", "Sta118", "Sta115", "Sta162", "Sta15", "Sta86", "Sta46", "Sta63",
              "Sta3", "Sta25", "Sta146", "Sta130", "Sta120", "Sta82", "Sta164", "Sta152", "Sta45"]
    line8 = [ "Sta73", "Sta148", "Sta60", "Sta91", "Sta32", "Sta116", "Sta92", "Sta31", "Sta93", "Sta72",
             "Sta95", "Sta90", "Sta26", "Sta17", "Sta101", "Sta136", "Sta137"]



    big2Json = {}
    for oneMonth in monthList:
        dataList = []

        print("正在遍历" + "--" + str(oneMonth) + "......")
        twoDict = {}

        for a in line2:
            index1 = line2.index(a)
            num1 = 0
            num2 = 0
            for b in line2:
                index2 = line2.index(b)
                onelist = oneDict[b][oneMonth]['data']
                if index2 <= index1:
                    for it in onelist:
                        if it['outSta'] in line2 and line2.index(it['outSta']) >= index1:
                            num1 += it['outNum']
                        else:
                            continue

                if index2 >= index1:
                    for it in onelist:
                        if it['outSta'] in line2 and line2.index(it['outSta']) <= index1:
                            num2 += it['outNum']
                        else:
                            continue
            dataList.append({'data1': num1, 'data2': num2})
            twoDict = {"data": dataList}
        big2Json[oneMonth] = twoDict




    print(big2Json)
    s = json.dumps(big2Json)
    f = open('templates/seven/bigJson2.json', 'w', encoding='UTF-8')
    f.write(s)
    f.close()
    print("构造bigJson.json成功！")
    return HttpResponse("构造成功！")

def show2(request):
    # person = {
    #     'name': 'xdm',
    #     'age': 19
    # }

    f = open('templates/seven/bigJson2.json', 'r', encoding='utf-8')
    bigJson = f.read()
    f.close()
    print("bigjson的类型为：" + str(type(bigJson)))
    oneDict = json.loads(bigJson)
    print("t2的类型：" + str(type(oneDict)))
    # year = 2020
    year = request.GET.get('year')
    month = request.GET.get('month')
    # month = 7
    time = str(year) + "/" + str(month)
    print("time : " + str(time))
    print("线路二")
    # inStation = 'Sta18'
    person = oneDict[time]
    # data = [
    #     {"outSta": "Sta1",
    #      "outNum": 41,
    #      },
    # ]
    #
    # person = {"data": data}
    # print("person的数据：")
    # print(person)
    # person_str = json.dumps(person)
    # response = HttpResponse(person_str , content_type='application/json; charset=utf-8')
    response = HttpResponse(json.dumps(person), content_type='application/json; charset=utf-8')
    return response

def show3(request):
    # person = {
    #     'name': 'xdm',
    #     'age': 19
    # }

    f = open('templates/seven/bigJson3.json', 'r', encoding='utf-8')
    bigJson = f.read()
    f.close()
    print("bigjson的类型为：" + str(type(bigJson)))
    oneDict = json.loads(bigJson)
    print("t2的类型：" + str(type(oneDict)))
    # year = 2020
    year = request.GET.get('year')
    month = request.GET.get('month')
    # month = 7
    time = str(year) + "/" + str(month)
    print("time : " + str(time))

    # inStation = 'Sta18'
    person = oneDict[time]
    # data = [
    #     {"outSta": "Sta1",
    #      "outNum": 41,
    #      },
    # ]
    #
    # person = {"data": data}
    # print("person的数据：")
    # print(person)
    # person_str = json.dumps(person)
    # response = HttpResponse(person_str , content_type='application/json; charset=utf-8')
    response = HttpResponse(json.dumps(person), content_type='application/json; charset=utf-8')
    return response

def show4(request):
    # person = {
    #     'name': 'xdm',
    #     'age': 19
    # }

    f = open('templates/seven/bigJson4.json', 'r', encoding='utf-8')
    bigJson = f.read()
    f.close()
    print("bigjson的类型为：" + str(type(bigJson)))
    oneDict = json.loads(bigJson)
    print("t2的类型：" + str(type(oneDict)))
    # year = 2020
    year = request.GET.get('year')
    month = request.GET.get('month')
    # month = 7
    time = str(year) + "/" + str(month)
    print("time : " + str(time))

    # inStation = 'Sta18'
    person = oneDict[time]
    # data = [
    #     {"outSta": "Sta1",
    #      "outNum": 41,
    #      },
    # ]
    #
    # person = {"data": data}
    # print("person的数据：")
    # print(person)
    # person_str = json.dumps(person)
    # response = HttpResponse(person_str , content_type='application/json; charset=utf-8')
    response = HttpResponse(json.dumps(person), content_type='application/json; charset=utf-8')
    return response




def show5(request):
    # person = {
    #     'name': 'xdm',
    #     'age': 19
    # }

    f = open('templates/seven/bigJson5.json', 'r', encoding='utf-8')
    bigJson = f.read()
    f.close()
    print("bigjson的类型为：" + str(type(bigJson)))
    oneDict = json.loads(bigJson)
    print("t2的类型：" + str(type(oneDict)))
    # year = 2020
    year = request.GET.get('year')
    month = request.GET.get('month')
    # month = 7
    time = str(year) + "/" + str(month)
    print("time : " + str(time))

    # inStation = 'Sta18'
    person = oneDict[time]
    # data = [
    #     {"outSta": "Sta1",
    #      "outNum": 41,
    #      },
    # ]
    #
    # person = {"data": data}
    # print("person的数据：")
    # print(person)
    # person_str = json.dumps(person)
    # response = HttpResponse(person_str , content_type='application/json; charset=utf-8')
    response = HttpResponse(json.dumps(person), content_type='application/json; charset=utf-8')
    return response

def show6(request):
    # person = {
    #     'name': 'xdm',
    #     'age': 19
    # }

    f = open('templates/seven/bigJson6.json', 'r', encoding='utf-8')
    bigJson = f.read()
    f.close()
    print("bigjson的类型为：" + str(type(bigJson)))
    oneDict = json.loads(bigJson)
    print("t2的类型：" + str(type(oneDict)))
    # year = 2020
    year = request.GET.get('year')
    month = request.GET.get('month')
    # month = 7
    time = str(year) + "/" + str(month)
    print("time : " + str(time))

    # inStation = 'Sta18'
    person = oneDict[time]
    # data = [
    #     {"outSta": "Sta1",
    #      "outNum": 41,
    #      },
    # ]
    #
    # person = {"data": data}
    # print("person的数据：")
    # print(person)
    # person_str = json.dumps(person)
    # response = HttpResponse(person_str , content_type='application/json; charset=utf-8')
    response = HttpResponse(json.dumps(person), content_type='application/json; charset=utf-8')
    return response


def show7(request):
    # person = {
    #     'name': 'xdm',
    #     'age': 19
    # }

    f = open('templates/seven/bigJson7.json', 'r', encoding='utf-8')
    bigJson = f.read()
    f.close()
    print("bigjson的类型为：" + str(type(bigJson)))
    oneDict = json.loads(bigJson)
    print("t2的类型：" + str(type(oneDict)))
    # year = 2020
    year = request.GET.get('year')
    month = request.GET.get('month')
    # month = 7
    time = str(year) + "/" + str(month)
    print("time : " + str(time))

    # inStation = 'Sta18'
    person = oneDict[time]
    # data = [
    #     {"outSta": "Sta1",
    #      "outNum": 41,
    #      },
    # ]
    #
    # person = {"data": data}
    # print("person的数据：")
    # print(person)
    # person_str = json.dumps(person)
    # response = HttpResponse(person_str , content_type='application/json; charset=utf-8')
    response = HttpResponse(json.dumps(person), content_type='application/json; charset=utf-8')
    return response

def show8(request):
    # person = {
    #     'name': 'xdm',
    #     'age': 19
    # }

    f = open('templates/seven/bigJson8.json', 'r', encoding='utf-8')
    bigJson = f.read()
    f.close()
    print("bigjson的类型为：" + str(type(bigJson)))
    oneDict = json.loads(bigJson)
    print("t2的类型：" + str(type(oneDict)))
    # year = 2020
    year = request.GET.get('year')
    month = request.GET.get('month')
    # month = 7
    time = str(year) + "/" + str(month)
    print("time : " + str(time))

    # inStation = 'Sta18'
    person = oneDict[time]
    # data = [
    #     {"outSta": "Sta1",
    #      "outNum": 41,
    #      },
    # ]
    #
    # person = {"data": data}
    # print("person的数据：")
    # print(person)
    # person_str = json.dumps(person)
    # response = HttpResponse(person_str , content_type='application/json; charset=utf-8')
    response = HttpResponse(json.dumps(person), content_type='application/json; charset=utf-8')
    return response
def showSeven(request):
    # person = {
    #     'name': 'xdm',
    #     'age': 19
    # }

    f = open('templates/seven/bigJson.json', 'r', encoding='utf-8')
    bigJson = f.read()
    f.close()
    print("bigjson的类型为：" + str(type(bigJson)))
    oneDict = json.loads(bigJson)
    print("t2的类型为：" + str(type(oneDict)))
    # year = 2020
    year = request.GET.get('year')
    month = request.GET.get('month')
    # month = 7
    time = str(year) + "/" + str(month)
    print("time : " + str(time))
    # inStation = 'Sta18'



    person = oneDict[time]
    # data = [
    #     {"outSta": "Sta1",
    #      "outNum": 41,
    #      },
    # ]
    #
    # person = {"data": data}
    # print("person的数据：")
    # print(person)
    # person_str = json.dumps(person)
    # response = HttpResponse(person_str , content_type='application/json; charset=utf-8')
    response = HttpResponse(json.dumps(person), content_type='application/json; charset=utf-8')
    return response


































